package com.yaorange.jk.job;

import com.yaorange.jk.entity.Contract;
import com.yaorange.jk.entity.User;
import com.yaorange.jk.service.ContractService;
import com.yaorange.jk.service.UserService;
import com.yaorange.jk.utils.MailUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author coach tam
 * @date 2017/11/13
 */
public class MailJob {

    private String defMail;
    @Autowired
    private ContractService contractService;

    @Autowired
    private UserService userService;

    public void sendMail(){
//        System.out.println("正在发送邮件:"+new Date());
        //查询出交货日期为今天的购销合同
        String now = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        List<Contract> contractList = contractService.findByDeliveryPeriod(now);
            //循环遍历每个购销合同，发送邮件
        if(contractList!=null)
        {
            for (Contract contract:contractList) {
                User user = userService.findById(contract.getCreateBy());
                String mail = user.getUserInfo().getEmail();
                System.out.println(mail);
                if(mail == null)
                {
                    //如果改合同的联系人邮件是空的，那么发给默认联系人
                    mail = defMail;
                }

                MailUtils.sendMail(mail,"提示:购销合同交货日期到了",contract.getContractNo()+"的交期到了，请尽快处理！");
            }
        }
    }

    public void setDefMail(String defMail) {
        this.defMail = defMail;
    }
}
